package com.example.demo.service.impl;

import com.example.demo.exceptions.DishNotFoundException;
import com.example.demo.model.Dishes;
import com.example.demo.model.Ingredients;
import com.example.demo.repository.DishesRepository;
import com.example.demo.repository.IngredientRepository;
import com.example.demo.service.DishesService;
import com.example.demo.util.RecognizeUtil;
import com.example.demo.util.hanLP;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.Optional;

@Service
public class DishesServiceImpl implements DishesService {
    @Autowired
    private RecognizeUtil reg;
    private hanLP hlp;
    private DishesRepository dishesRepository;

    @Autowired
    private IngredientRepository ingredientRepo;

    @Override
    public List<Dishes> getAllDishes() {
        return dishesRepository.findAll();
    }

    @Override
    public Dishes getDishesById(Long dishId) {
        return dishesRepository.findByDishId(dishId);
    }

    @Override
    public Dishes addDishes(Dishes dishes) {
        return dishesRepository.save(dishes);
    }

    @Override
    public Dishes recognizeFood(String imageBase64)
    {
        Dishes dish = reg.Recognize(imageBase64);
        dishesRepository.save(dish);
        return dish;
    }

    @Override
    public void incrementDishViewCount(Long dishId) {
        Optional<Dishes> optionalDish = dishesRepository.findById(dishId);
        if (optionalDish.isPresent()) {
            Dishes dish = optionalDish.get();
            dish.setViewCount(dish.getViewCount() + 1);
            dishesRepository.save(dish);
        } else {
            throw new DishNotFoundException("Dish not found with id: " + dishId);
        }
    }

    @Override
    // 根据食材名称获取菜品列表
    public List<Dishes> getDishesByIngredientName(String ingredientName) {
        List<Dishes> dishes = new ArrayList<>();
        List<Dishes> allDishes = dishesRepository.findAll();

        for (Dishes dish : allDishes) {
            for (Ingredients ingredient : dish.getDIngredients()) {
                if (ingredient.getName().equals(ingredientName)) {
                    dishes.add(dish);
                    break;
                }
            }
        }

        return dishes;
    }

    @Override
    // 根据食材名称对菜品进行分类
    public Map<String, List<Dishes>> classifyDishesByIngredient() {
        List<Dishes> dishes = dishesRepository.findAll();
        Map<String, List<Dishes>> classifiedDishes = new HashMap<>();

        for (Dishes dish : dishes) {
            List<Ingredients> ingredients = dish.getDIngredients();

            for (Ingredients ingredient : ingredients) {
                String ingredientName = ingredient.getName();
                List<Dishes> classifiedList = classifiedDishes.computeIfAbsent(ingredientName, k -> new ArrayList<>());
                classifiedList.add(dish);
            }
        }

        return classifiedDishes;
    }
    @Override
    public List<Dishes> RecommandForUserInfo(String description){
        List<Dishes> dishes = hlp.Search(description);
        return dishes;
    }

}
